﻿using MediatR;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.BinLocationCommands;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.BinLocationCommandsHandlder
{
    public class BinLocationSelectCommandHandlder : IRequestHandler<BinLocationSelectCommand, ApiResult<BinLocationPageDTO>>
    {
        private readonly ILogger<BinLocationSelectCommandHandlder> _logger;
        private readonly IWarehouseLocationRepository _warehouseLocationRepository;
        private readonly IBinLocationRepository _binLocationRepository;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        public BinLocationSelectCommandHandlder(IWarehouseLocationRepository warehouseLocationRepository, ILogger<BinLocationSelectCommandHandlder> logger, IBinLocationRepository binLocationRepository, IWarehouseInfoRepository warehouseInfoRepository)
        {
            _warehouseLocationRepository = warehouseLocationRepository;
            _logger = logger;
            _binLocationRepository = binLocationRepository;
            _warehouseInfoRepository = warehouseInfoRepository;
        }

        public async Task<ApiResult<BinLocationPageDTO>> Handle(BinLocationSelectCommand request, CancellationToken cancellationToken)
        {
            ApiResult<BinLocationPageDTO> result = new ApiResult<BinLocationPageDTO>();
            try
            {
                _logger.LogInformation("显示查询分页库位列表成功");
                BinLocationPageDTO binLocation = new BinLocationPageDTO();
                var list = from a in (await _binLocationRepository.GetAllAsync())
                           join b in (await _warehouseInfoRepository.GetAllAsync()) on a.WarehouseId equals b.WarehouseId
                           join c in (await _warehouseLocationRepository.GetAllAsync()) on a.WarehouseLocationId equals c.WarehouseLocationId
                           where a.IsDelete == false && b.IsDelete == false
                           select new BinLocationsData
                           {
                               BinLocationId = a.BinLocationId,
                               BinLocationNo = a.BinLocationNo,
                               BinLocationName = a.BinLocationName,
                               WarehouseName = b.WarehouseName,
                               WarehouseId = a.WarehouseId,
                               IsDefault = a.IsDefault,
                               IsDisable = a.IsDisable,
                               CreateName = a.CreateName,
                               CreateTime = a.CreateTime,
                               BinLocationRemarks = a.BinLocationRemarks,
                               WarehouseLocationName = c.WarehouseLocationName,
                           };
                if (!string.IsNullOrEmpty(request.binLocationNo))
                {
                    list = list.Where(x => x.BinLocationNo.Contains(request.binLocationNo));
                }
                if (!string.IsNullOrEmpty(request.binLocationName))
                {
                    list = list.Where(x => x.BinLocationName.Contains(request.binLocationName));
                }
                if (request.warehouseId > 0)
                {
                    list = list.Where(x => x.WarehouseId == request.warehouseId);
                }
                binLocation.List = list.OrderByDescending(x => x.CreateTime).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                binLocation.TotalCount = list.Count();
                binLocation.TotalPage = (int)Math.Ceiling(binLocation.TotalCount * 1.0 / request.pageSize);
                result.Data = binLocation;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
